package top.lyc829.leetcode.T415;

public class Solution {
    public String addStrings(String num1, String num2) {

        int i = num1.length() - 1;
        int j = num2.length() - 1;
        int flag = 0;

        StringBuffer buffer = new StringBuffer();


        // flag > 0， 防止出现1 + 9的情况，首位发生进位被丢弃
        while (i >= 0 || j >= 0 || flag > 0) {
            char x = i >= 0 ? num1.charAt(i--) : '0';
            char y = j >= 0 ? num2.charAt(j--) : '0';
            int tmp = x - '0' + y - '0';
            tmp += flag;
            buffer.append(tmp % 10);
            flag = tmp / 10;
        }

        buffer.reverse();

        return buffer.toString();
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        String res = s.addStrings("1", "9");
        System.out.println(res);
    }

}
